package com.example.student.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.student.generated.entity.Student;
import com.example.student.generated.entity.StudentCourse;
import com.example.student.generated.mapper.StudentMapper;
import com.example.student.generated.service.IStudentCourseService;
import com.example.student.service.MyStudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MyStudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements MyStudentService {
    @Resource
    private IStudentCourseService myStudentCourseService;

    /**
     * 删除学生以及其选课记录
     * @param id
     * @return (success)删除成功 (failure)删除失败
     */
    @Override
    public boolean deleteOne(Long id) {
            //先删除学生对应的选课记录
            myStudentCourseService.remove(Wrappers.<StudentCourse>lambdaQuery().eq(StudentCourse::getStuId, id));
            //删除学生记录
        return removeById(id);
    }

    /**
     * 批量删除学生以及其选课记录
     * @param studentList
     * @return (success )返回删除成功的学生Id (failure)空字符串
     */
    @Override
    public String deleteBatch(List<Student> studentList) {
        StringBuilder res=new StringBuilder();
        for (Student student : studentList) {
            //先删除学生对应的选课记录
            myStudentCourseService.remove(Wrappers.<StudentCourse>lambdaQuery().eq(StudentCourse::getStuId, student.getId()));
            //删除学生记录
            boolean flag = removeById(student.getId());
            if(flag) res.append(student.getId()).append(" ");
        }
        return res.toString();
    }
}

